package com.watermeter.android.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.watermeter.android.R;
import com.watermeter.android.activity.DownloadRouteActivity;
import com.watermeter.android.activity.YesCBActivity;
import com.watermeter.android.utils.Util;


public class SelectDatePopupWindow extends PopupWindow implements
		View.OnClickListener {

	private ListView lisViewtPop;

	private View mMenuView;
	
//	private String[] str;
	public SelectDatePopupWindow(Activity context, final String[] str, final TextView textView, final int temp) {
		super(context);

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mMenuView = inflater.inflate(R.layout.select_trading_pop, null);
		lisViewtPop = (ListView) mMenuView.findViewById(R.id.dateListview);

		lisViewtPop.setAdapter(new ArrayAdapter<String>(context,
                android.R.layout.simple_list_item_1, str));
		lisViewtPop.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int postion,
					long arg3) {
				// TODO Auto-generated method stub

					textView.setText(str[postion]);
				if (temp==1) {
					DownloadRouteActivity.activity.setReh(str[postion]);
				}else{
					YesCBActivity.activity.setReh(str[postion]);
				}
				dismiss();
			}
		});
		// 设置SelectPicPopupWindow的View
		this.setContentView(mMenuView);
		// 设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(Util.px2dip(context, 100));
		// 设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(LayoutParams.WRAP_CONTENT);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		// 设置SelectPicPopupWindow弹出窗体动画效果
//		 this.setAnimationStyle(R.style.AnimBottom);
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(0xffffffff);
		// 设置SelectPicPopupWindow弹出窗体的背景
		this.setBackgroundDrawable(dw);
		
		// mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
		mMenuView.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {

				int height = mMenuView.findViewById(R.id.pop_layout).getTop();
				int y = (int) event.getY();
				if (event.getAction() == MotionEvent.ACTION_UP) {
					if (y < height) {
						dismiss();
					}
				}
				return true;
			}
		});

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {

		}
	}

	@Override
	public void dismiss() {
		super.dismiss();
	}
}
